/**

 * User: seeker910
 * Date: 13-10-24
 * Time: 下午10:21

 *
 * */
Rsd.define('Rsd.form.ComboBox', {
    extend: 'Rsd.control.Component',
    requires: ['Rsd.control.Table'],
    xtype: 'combobox',
    textMember: 'text',
    /**
     * @description 格式化字符串
     */
    textFormatString:'',
    valueMember: 'value',
    colorMember: 'color',
    margin:'2 0 2 0',
    height:30,
    width:300,
    ctrlCls: 'x-control-combobox',
    selectedIndex:-1,
    selectedItem: null,
    /*
    * 仅在手动选择数据时，会被触发。textChanged 别名
     * */
    selectedItemChanged:null,
    /*
    * 仅在手动选择数据时，会被触发。
    * onchange事件 
    * onchange 事件 (兼容)
    * */
    textChanged:null,
      /*
    * onchange 事件 (兼容)
    * */
    onchange:null,
    /**
     * []
     */
    //dataSource: null,
    /**
     * 
     */
    readonly: false,
    disabled:false,
    required:null,
    /*
    * nullText == null 时不显示
    * */
    nullText: '--请选择--',
    ctrlTagName: 'select',
    /*
     * */
    constructor: function ComboBox (config) {
        this.apply(config||{});  
    },
    /*
     * */
    initComponentEx: function initComponentEx() {
        var me = this;
        me.callParent();

        me._bindOption();

        me.ctrl.element.name = me.dataIndex;
        //me.ctrl.element.readonly = me.readonly;
        me.ctrl.element.disabled = me.disabled;
        me.ctrl.element.required = me.required?'required':'';
        me.ctrl.element.placeholder = me.placeholder;
        me.ctrl.element.autocomplete="off";
 
        if(me.readonly){
            me.dom.style.pointerEvents = 'none';
            me.ctrl.element.style.pointerEvents = 'none';
        }
        me.ctrl.element.onchange = function (evt) {
            me.selectedIndex= this.selectedIndex;
            me.selectedItem = me.getSelectedItem();
            me.funApplyByIOC(me.selectedItemChanged,[me.selectedItem,evt]);
            me.funApplyByIOC(me.textChanged||me.onchange,[me,evt]);

            //处理联动字段 ，联动字段可以有多个
            if (me.linkages && me.linkages.length > 0) {

                for (var i in me.linkages) {
                    var linkage = me.linkages[i];
                    var linkage_value = me.selectedItem[linkage.valueMemberName];
                    var linkage_field = me.parent.getItemByName(linkage.targetField);
                    //console.log(linkage,me.selectedItem, linkage_field, linkage_value);
                    linkage_field.setValue(linkage_value);
                }
            } 
        }

        return;

    },
    /*
    * 
    */
    onAfterInit: function onAfterInit() {
        this.callParent();
        var me = this;
        me.dom.style.textAlign = 'left';

        //控件可能在初始化前被赋值，所有初始化后需要重新赋值 
        
        //优先：使用 __value 赋值
        if (me.__value !== undefined) {//此处不可使用getValue()方法，因为getValue优先获取ctrl.value ,此时ctrl.value未设置为空

            me.ctrl.element.value = me.__value; 
            return;
        }

        //其次：依据 selectedIndex 设置 
        if(me.seclectIndex !=null
            && me.seclectIndex != undefined
            && me.seclectIndex > -1)
        {
            me.select(me.seclectIndex);
            return ;
        }
        //
        me.setValue(null);
    },
    /**
     * 
     */
    _bindOption:function _bindOption()
    {
        if(this.ctrl.element)
        {
            this.ctrl.element.options.length = 0;
        }
 
        if(this.ctrl.element && this.nullText)
        {
            this.ctrl.element.options.add(new Option(this.nullText,''));
        }

        for(var i in this.dataSource)
        {
            var item = this.dataSource[i];

            if(this.ctrl.element)
            {

                var op = document.createElement("option");
                op.style.color = Rsd.isObject(item)?item[this.colorMember]:null;
                op.setAttribute("value",Rsd.isObject(item)?item[this.valueMember]:item);
                var txt = '';
                if(!Rsd.isEmpty(this.textMember))
                {
                    txt = Rsd.isObject(item)?item[this.textMember]:item;
                } 
                if(!Rsd.isEmpty(this.textFormatString))
                {
                    txt = Rsd.template(this.textFormatString,item);
                }
                op.appendChild(document.createTextNode(txt));

                this.ctrl.element.options.add(op);
            }
        }
    },
    /*
    *
    * */
    loadData:function loadData(dataSource)
    {
        if(dataSource===undefined)
        {
            return;
        }
        //debugger;
        this.dataSource  = dataSource||this.dataSource||[];

        this._bindOption();

        if(this.ctrl.element && this.value!= undefined && this.value != '')
        {
            this.ctrl.element.value = this.value;
            return;
        }

        if(this.selectedIndex != undefined
            && this.selectedIndex !=null
            && !(this.selectedIndex < 0)
        )
        {
            this.select(this.selectedIndex);
            return;
        }
 
    },
    
    /*
     * 设置值时，不会触发 selectedItemChanged
     * */
    select: function (index) {

        if(this.ctrl.element == null)
        {
            return;
        }

        if (index < this.ctrl.element.options.length ) {

            this.ctrl.element.selectedIndex = index;

        }
        else
        {
            this.selectedIndex = index;
           //Rsd.error('set selected index is overflow.');
        }

    },

    /*
     *
     * */
    getSelectedItem: function getSelectedItem() {

        var i = 0;
        if(this.ctrl.element)
        {
            i = this.ctrl.element.selectedIndex;
        }

        if(this.nullText)
        {
            i=i-1;
        }
        if(i<0)
        {
            return null;
        }

        var item = this.dataSource[i];

        return item;
    },

    /*
     * 设置值时，不会触发 selectedItemChanged
     * */
    setValue: function (value) {

        if (arguments.length == 0) {

            return;
        }

        this.__value = value;

        if(this.ctrl.element)
        { 
            this.ctrl.element.value = value; 
            //console.log('set combobox-value',value);
        }

        return;

    },
    /** 
    * @public
    * 公开方法，获取控件值 
    */
    getValue:function getValue()
    {

        if(this.ctrl.element)
        {
            return this.ctrl.element.value;
        }

        return this.__value;
    },
    /*
     * defaultValue 是控件初次加载时设置的值，可用于判断控件值是否改变。
     * */
    getDefaultValue: function getDefaultValue() {
        var me = this;
        return me.ctrl.element ? me.ctrl.element.defaultValue : me.value
    },
    /*
    *
    * */
    checkValue:function checkValue() {
        //debugger;
        this.error='';
        this.ctrl.element.title = this.tip ||'';
        this.removeCls('ctrl','x-control-error');
        if(this.required)
        {

            if(this.ctrl.element.selectedIndex< 0 || Rsd.isEmpty(this.ctrl.element.value))
            {
                this.error = '【' + this.label.content + '】必填';
                this.ctrl.element.title = this.error;
                this.addCls('ctrl','x-control-error');
                return false;
            }

        }
        return true;
    },
    /*
    * */
    makeControl:function makeControl(config,row,state)
    {           //debugger;
        var _config = config ||{};
        var _editable = _config.editable;
        var _value = row[_config.dataIndex||_config.name];
        var _valueMember = _config.valueMember || 'value';
        var _textMember = _config.textMember || 'text';
        var _colorMember = _config.colorMember || 'color';
        var _dataSource = _config.dataSource||[];

        if(!Rsd.isArray(_dataSource))
        {
            throw new Error('config [dataSource] is null or empty.');
        }
        if(Rsd.isNullOrUndefined(_valueMember))
        {
            throw new Error('config [valueMember] is null or empty.');
        }
        if(Rsd.isNullOrUndefined(_textMember))
        {
            throw new Error('config [textMember] is null or empty.');
        }
        if(!_config.__mapping)
        {
            _config.__mapping = {};
            var _item = null;
            for(var i=0;i < _dataSource.length ;i++)
            {
                _item = _dataSource[i];
                _config.__mapping[_item[_valueMember]] = _item;
            }
        }
        var _ctrl = null;
        if(_editable && state)
        {
            _ctrl = document.createElement('select');
            _ctrl.onchange=function(){
                //debugger;
                var _v= _ctrl.options[_ctrl.selectedIndex].value;
                row[_config.dataIndex||_config.name] = Rsd.isEmpty(_v)?'':_v;
            };
            _ctrl.style.width = '80%';
            var _ds = _config.dataSource;
            //console.log(_ds);
            var _op = new Option();
            _op.text = '--请选择--';
            _op.value = null;
            _ctrl.options.add(_op);
            if(_ds)
            {
                for(var i = 0 ;i< _ds.length ;i++)
                {
                    _op = new Option();
                    _op.text = _ds[i].text;
                    _op.value = _ds[i].value;
                    _op.selected = _value == _ds[i].value;
                    _ctrl.options.add(_op);
                }
            }

        }
        else
        {
            _ctrl = document.createElement('label');
            _ctrl.style.position = 'relative';
            _ctrl.style.width = '100%';
            _ctrl.style.height = '100%';

            var _timer = 0;
            if(_config.__mapping.hasOwnProperty(_value))
            {
                _timer = 10;
            }
            else
            {
                _timer = 500;
            }
            setTimeout(function () {
                var _item = _config.__mapping[_value];
                var _txt = _value;
                if(_item)
                {
                    _txt = _item[_textMember];
                    if(_colorMember && _item.hasOwnProperty(_colorMember))
                    {
                        _ctrl.style.color = _item[_colorMember];
                    }
                }

                _ctrl.appendChild(document.createTextNode(_txt));


            },_timer);
        }
        return [_ctrl];
    }
},function (type) {

    //
    var _valueGetter = function () {
        return this.getValue();
    };

    var _valueSetter = function (value) {
        this.setValue(value);
    };
    this.defineProperty(type,"value", _valueGetter, _valueSetter,true);
 

    //
    var _dataSourceGetter = function () {

        if (!this.hasOwnProperty('__dataSource')) {
            this.__dataSource = [];
        }
        return this.__dataSource;
    };

    var _dataSourceSetter = function (value) {
 
        if(value == this.dataSource)
        {
            return;
        }
        if(!Rsd.isArray(value))
        {
            console.error('Set Rsd.form.ComboBox property dataSource error,the value is not array type',value);
            return;
        }
        this.__dataSource = value;
        this._bindOption(); 
    };
    this.defineProperty(type,"dataSource", _dataSourceGetter, _dataSourceSetter,false);
});

